package org.fcitmuk.openclinica;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.fcitmuk.db.util.PersistentHelper;
import org.fcitmuk.db.util.Persistent;


/**
 * This class encapsulates all data about subjects. Can serve as the subject in-memory database.
 * This class is the one serialized from the subject server to the device.
 * 
 * @author Daniel
 *
 */
public class SubjectData implements Persistent{

	/** The list of subjects. */
	private SubjectList subjects = new SubjectList();

	/** The list of subject database fields. */
	private SubjectFieldList fields = new SubjectFieldList();

	/** The list of subject database field values. */
	private SubjectFieldValueList fieldValues = new SubjectFieldValueList();

	public SubjectData(){

	}

	public SubjectData(SubjectList subjects, SubjectFieldList fields, SubjectFieldValueList fieldValues) {
		super();
		this.subjects = subjects;
		this.fields = fields;
		this.fieldValues = fieldValues;
	}

	public SubjectFieldList getFields() {
		return fields;
	}

	public void setFields(SubjectFieldList fields) {
		this.fields = fields;
	}

	public SubjectFieldValueList getFieldValues() {
		return fieldValues;
	}

	public void setFieldValues(SubjectFieldValueList fieldValues) {
		this.fieldValues = fieldValues;
	}

	public SubjectList getSubjects() {
		return subjects;
	}

	public void setSubjects(SubjectList subjects) {
		this.subjects = subjects;
	}

	/** 
	 * Reads the subject data object from the supplied stream.
	 * 
	 * @param dis - the stream to read from.
	 * @throws IOException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public void read(DataInputStream dis) throws IOException, InstantiationException, IllegalAccessException {
		subjects.read(dis);
		fields.read(dis);
		fieldValues.read(dis);
	}

	/** 
	 * Writes the subject data object to the supplied stream.
	 * 
	 * @param dos - the stream to write to.
	 * @throws IOException
	 */
	public void write(DataOutputStream dos) throws IOException {
		subjects.write(dos);
		fields.write(dos);
		fieldValues.write(dos);
	}
}
